
*===================================================================*
*   BIHAR EVALUATION OF SOCIAL FRANCHISING AND TELEMEDICINE (BEST)
*                Processing Provider's Interview data
**===================================================================*

* Open Data
  use "$rawdatap1\deidentified_provider interview_141111", clear

* Check number of clusters (80) and observations (395)
  duplicates report c_id
  assert r(unique_value)==80
  assert r(N)==395

* Source
  gen     source = "Provider Survey"
  lab var source "Data source"

* Wave
  gen     wave = 1
  lab var wave "Wave"

* Sample Selection
  gen     sample1 = 0 
  replace sample1 = 1 if q2==1
  lab def sample 0 "incomplete" 1 "complete"
  lab val sample1 sample
  lab var sample1 "Sample"

* Provider ID
  gen     prov_id = p_code
  lab var prov_id "Provider ID"

* Location Variables
  lab var district "District"
  order   district, last
  lab var block "Block"
  order block, last
  gen     cluster = c_id
  lab var cluster "Cluster"
  gen     village = n_v
  lab var village "Village"

* Treated Clusters
  sort cluster
  merge m:1 cluster using "$rawdatap2\Listing_treatment_clusters"
  gen     treat = 1 if _merge==3
  replace treat = 0 if _merge==1
  replace treat = 1 if (cluster==12 | cluster==94 | cluster==429)
  lab var treat "=1 if treated"
  drop if _merge==2
  drop _merge

* Interview related variables
  gen     int_status  = q2
  lab var int_status "status of interview"
  gen     reason = q3
  lab var reason "reason for refusing to give the interview"
  gen     tnvisit= q4
  lab var tnvisit "Total # of visits made to the health facility"

  gen     int_code = q5
  lab var int_code "Interviewer code"
  gen     check_code = q6
  lab var check_code "Scrutinizer code"
  gen     sup_code = q7
  lab var sup_code "Supervisor code"

* Interview date
  gen     int_date=mdy(mm, dd, yyyy)
  format  int_date %d
  lab var int_date "date of interview"

* Non- medical practitioners - all missing
  drop a07_1-a07_7

* Label yes/no
  label define yesno 0 "no" 1 "yes"

* Check providers per cluster
  bys cluster sample1: gen obs=_n 
  sum obs if sample==1
  drop obs

***********************************************
* SOCIOECONOMICS CHARACTERISTICS
***********************************************

* Age
  gen     age = a06_1
  lab var age "provider age"

* Sex
  recode a06_2 (2=0), gen(sex)
  lab var sex "=1 if male"

* Experience
/*a06_5: experience as medical practitioner: in years*/
  gen     experience = a06_5
  lab var experience "provider experience (years)"

* Medical Qualifications
  gen     educa_med = a06_3a
  replace educa_med = 8 if a06_3a>=8 & a06_3a<=77
  replace educa_med = 0 if a06_3a==99
  lab var educa_med "Highest Medical Qualification"
  #delimit ;
  lab def educa_med 
  0 "No degree"
  1 "mbbs"
  2 "bds (dentist)"
  3 "bams"
  4 "bums"
  5 "bhms/dhms"
  6 "md/ms/dnb/etc (allopathic)"
  7 "md/etc (non allopathic)"
  8 "Other";
  #delimit cr
  lab val educa_med educa_med

* Dummies Medical qualification
  gen mbbs     = inlist(a06_3a, 1, 6) if a06_3a!=.
  gen bams     = inlist(a06_3a, 3, 4, 5, 7, 8) if a06_3a!=.
  gen semi     = inlist(a06_3a, 9, 10, 12) if a06_3a!=. 
  gen informal = inlist(a06_3a, 2, 11, 13, 77) if a06_3a!=. 
  gen none     = inlist(a06_3a, 99) if a06_3a!=.

  label var mbbs "MBBS, MD/MS/DNB (allopathic)"
  label var bams "BAMS/BHMS/BUMS/NON-ALLOPATHIC M/Dip. in Ayurvedic"
  label var semi "Semi trained"
  label var informal "Informal training"
  label var none "None"

* Medical/No Medical Qualification
  gen     mqualif = 0 if none==1 | informal==1 | semi==1
  replace mqualif = 1 if mbbs==1 | bams==1
  lab var mqualif "High/Low medical qualification"
  lab def mqualif 0 "No Medical Qualification" 1 "Medical Qualification"
  lab val mqualif mqualif

* Non-Medical Qualifications
  gen     educa_nomed = a06_4a
  recode  educa_nomed (88=5)
  lab var educa_nomed "Higuest Non-medical Qualification"
  lab def educa_nomed 1 "<12th standard" 2 "12th standard pass" 3 "Graduate" 4 "Post-graduate" 5 "Any Other"
  lab val educa_nomed educa_nomed

  gen     high_school = inlist(a06_4a, 2,3,4)
  lab var high_school "12 years or more of non-medical qualifications"


***********************************************
* MODULE A: WORKING CHARACTERISTICS
***********************************************

* Working Hours in facilities
  egen    work_hs_fac = rsum(a06_6 a06_7), missing
  lab var work_hs_fac "Total hs. per week he works in facilities"

* Working Hours visiting homes
  egen    work_hs_vis = rsum(a06_8 a06_9), missing
  lab var work_hs_vis "Total hs. per week he works visiting homes"
  
* Total woking hours
  egen    work_hs = rsum(work_hs_fac work_hs_vis), missing
  lab var work_hs "Total working hours per week"

* Task performed
  foreach var of varlist a06_10a-a06_10f {
    recode `var' (0=.)
    tab `var'
    }

  egen miss = rowmiss(a06_10a-a06_10f)
  forvalues n=1/8 { 
    egen    task_`n' =anymatch(a06_10a-a06_10f), v(`n')
    replace task_`n' =. if miss==6
    }

  lab var task_1 "=1 if consultation with patient"
  lab var task_2 "=1 if administering treatments"
  lab var task_3 "=1 if selling drugs"
  lab var task_4 "=1 if laboratory related duties"
  lab var task_5 "=1 if administrative duties"
  lab var task_6 "=1 if cleaning duties"
  lab var task_7 "=1 if ownership"
  lab var task_8 "=1 if others"
  drop miss

* Type of diseases treated
  replace a08_othe = "" if a08_othe == "0" | a08_othe == "8"
  foreach var of varlist a08a-a08f {
    recode `var' (0=.) (88=7)
    tab `var'
    }

  egen miss = rowmiss(a08a-a08f)
  forvalues n=1/7 {
    egen    disease_`n'=anymatch(a08a-a08f), v(`n')
    replace disease_`n'=. if miss==6
    }

  lab var disease_1 "Fever"
  lab var disease_2 "Cough"
  lab var disease_3 "Diarrhea"
  lab var disease_4 "Pneumonia"
  lab var disease_5 "Tubercosis"
  lab var disease_6 "VL"
  lab var disease_7 "Others"
  drop miss

* Run camps?
  recode a09 (2=0), gen(camps)
  lab var camps "=1 if facility run camps"
  lab val camps yesno

* Last time camp was run
  foreach var of varlist a10mm1-a10yy3 {
    recode `var' 0 = .
    }
  egen    last_camp=rmax(a10yy1 a10yy2 a10yy3)
  lab var last_camp "last time camp was run (year)"

* Partnership/Agreement with health care org.
  recode a11 (2=0), gen(health_part)
  lab var health_part "=1 if partnership with health organization"
  lab val health_part yesno

* Desire of Partnership/Agreement
  recode a12 (2=0), gen(part_desire)
  lab var part_desire "=1 if a partnership is desire"
  lab val part_desire yesno

* Training
  recode a13 (2=0), gen(training)
  lab var training "=1 if someone in facility received training"
  lab val training yesno


***********************************************
* MODULE B and D - FACILITY CHARACTERISTICS
***********************************************
* Facility ownership
  gen     fac_rent=(b02==1) if b02!=.
  lab var fac_rent "=1 if facility is rented"

  gen     fac_owner=(b02==2) if b02!=.
  lab var fac_owner "=1 if ownership of facility"

  gen     fac_free=(b02==3) if b02!=.
  lab var fac_free "=1 if facility is free"

* Owners medical facilities
/*b01: how many other medical facilities are run by the owners. 
  NOTE: Looks like the question was interpreted as how many 
  facilities are run by the owner and not how many OTHER. I changed 
  the code to make comparable to second round*/
  gen     fac_num=0 if b01==4
  replace fac_num=0 if b01==1
  replace fac_num=1 if b01>=2 & b01<=3
  lab var fac_num "=1 if owner run 2 or more facilities"

* Facility as part of a chain of clinics
  recode b03 (2=0), gen(fac_chain)
  lab var fac_chain "=1 if facility is part of a chain of clinics"
  lab val fac_chain yesno

* Electricity
  recode d01 (2=0), gen(electricity)
  lab var electricity "=1 if facility has electricity"
  lab val electricity yesno

* Power cut frequency 
  recode d02 (2 3 = 0), gen (power_cut)
  lab var power_cut "=1 if experience power cut more than once a day"

* Power cut duration - last time
  gen     powercut_dur = d03hh + d03mm/60
  lab var powercut_dur "duration of last time power cut (hours)"

* Has power backup
  recode d04 (2=0), gen(power_backup)
  lab var power_backup "=1 if has power backup"

* Type of medicine practiced
  foreach var of varlist b04a-b04e {
    recode `var' (0=.) (8=6)
    }

  egen miss=rowmiss(b04a-b04e)
  forvalues n=1/6 {
    egen    treatment_type_`n'=anymatch(b04a-b04e), v(`n')
    replace treatment_type_`n'=. if miss==6
    }

  lab var treatment_type_1 "Allopathic"
  lab var treatment_type_2 "Homeopathic"
  lab var treatment_type_3 "Ayurvedic"
  lab var treatment_type_4 "Unani"
  lab var treatment_type_5 "Siddha"
  lab var treatment_type_6 "Others"
  drop miss

* Facility ownership (sector)
  gen     fac_gov = (b05==1) if b05!=.
  lab var fac_gov "=1 if facility is from government"
  lab val fac_gov yesno

* Age of facility
  recode b06mm 0 = .
  la var b06mm  "b06 date of inception of the facility (month)"
  la var b06mma "b06 date of inception of the facility (month) - missing specify"
  la var b06yy  "b06 date of inception of the facility (year)"
  la var b06yya "b06 date of inception of the facility (year) - missing specify"

  gen    interview=ym(yyyy, mm)
  format interview %tm
  gen    fac_birth=ym(b06yy, b06mm)
  format fac_birth %tm
  gen     fac_age = (interview-fac_birth)/12
  replace fac_age = (yyyy-b06yy) if fac_age==. & b06yy!=.
  lab var fac_age "Age of facility in years"
  drop interview fac_birth

* Number of consulting rooms
  gen     room_num=b07
  lab var room_num "Number of consulting rooms in facility"

* Night-care facility?
  recode b09 (2=0), gen(fac_night)
  lab var fac_night "=1 if night care facility"
  lab val fac_night yesno

* Number of beds for day care/overnight stay
  gen     bed_day = b08
  lab var bed_day "Number of Day Beds"

  gen     bed_night = b10
  lab var bed_night "Number of beds for overnight stay"

* Have X-ray?
  recode b11 (2=0), gen(x_ray)
  lab var x_ray "=1 if facility has x ray"
  lab val x_ray yesno

* Have laboratory testing provisions?
  recode b12 (2=0), gen(lab_test)
  lab var lab_test "=1 if having laboratory testing provitions"
  lab val lab_test yesno

* Provide Any test?
  foreach var of varlist b13a-b13g {
    recode `var' (0=.) (88=9)
    }

  encode b13_othe, gen(b13_othe2)
  order b13_othe2, before(b13_othe)
  egen test=rowmiss(b13a-b13_othe2)
  replace test=1 if test<8
  replace test=0 if test==8
  lab var test "=1 if facility provide any test"
  lab val test yesno
  drop b13_othe2

* Type of test provided
  egen miss=rowmiss(b13a-b13g)
  forvalues n=1/9 {
    egen test_`n'=anymatch(b13a-b13g), v(`n')
    replace test_`n'=. if miss==7
    }

  lab var test_1 "Blood test"
  lab var test_2 "Haemoglobin test"
  lab var test_3 "TLC test"
  lab var test_4 "DLC test"
  lab var test_5 "Urime examination"
  lab var test_6 "Stool microscope examination"
  lab var test_7 "Sputum exam. for tuberculosis"
  lab var test_8 "Rapid VL test"
  lab var test_9 "Other test"
  drop miss

***********************************************
* MODULE C - MARKET
***********************************************
* Number of village patients come from (apart from the one where facility is located?)
  foreach var of varlist c01a-c01e {
    replace `var' = "" if regexm(`var',"0")
    replace `var' = "" if regexm(`var',"1")
    }

  egen    village_patient=rownonmiss(c01a-c01e), strok
  lab var village_patient "Number of villages from which patients have come"

* Distances
  gen     dist1 = c02
  lab var dist1 "Distance to the Nearest HCP (Kms)"
  gen     dist2 = c03
  lab var dist2 "Distance to the 2nd nearest HCP (Kms)"
  gen     dist3 = c04
  lab var dist3 "Distance to the 3rd nearest HCP (Kms)"
  gen     dist4 = c05
  replace dist4 =. if c05==99
  lab var dist4 "Distance from where provider get the most patients (Kms)"

* Patient Volume
  gen     pat_winter = c005
  lab var pat_winter "Number of patients attended per day in winter"
  gen     pat_summer = c06
  lab var pat_summer "Number of patients attended per day in summer"
  gen     pat_pmoonson = c07
  lab var pat_pmoonson "Number of patients attended post-moonson"
  gen     pat_yesterday = c08
  lab var pat_yesterday "Number of patients attended yesterday"

  egen    npatient=rmean(pat_winter pat_summer pat_pmoonson)
  lab var npatient "Average caseload per day all seasons"


***********************************************
* MODULE E - PRICING
***********************************************
* Consultation Fee
  gen     c_fee = e01
  lab var c_fee "First consultation fee (excluding lab tests)"

* Percentage of Consultation fee that covers basic medicines (%)
  gen     prc_cfee = e02
  replace prc_cfee=. if c_fee==0
  lab var prc_cfee "% of fee that covers basic medicines"

* Follow-up Consultation Fee
  gen     fu_fee = e03
  lab var fu_fee "Follow-up consultation fee"
  
* Percentage of Follow-up Consultation fee that covers basic medicines
  gen     prc_fufee = e04
  replace prc_fufee=. if fu_fee==0
  lab var prc_fufee "% of follow-up fee that covers basic medicines"

* Medicines prices 
  local numlist "05 06 07 08 09 10 11 12 13 14 16" 
  local i=1
  foreach n of local numlist {
  gen price_`i'=e`n' if e`n'!=9999
  local i = `i'+1
    }

  lab var price_1 "ORS packet"
  lab var price_2 "Sodium Stibo Gluconate (100 mg/ml vial)"
  lab var price_3 "Paracetamol 5 mg"
  lab var price_4 "Septran Syrup (40 mg/5 ml of trimethoprim)"
  lab var price_5 "Amoxycilin Dispersible Tablet (250 mg)"
  lab var price_6 "Antibiotic Tablet for diarrhea"
  lab var price_7 "Sodium Antimony Gluconate (100 mg/ml per vial)"
  lab var price_8 "Amphotericin-b (100 mg/ml per vial)"
  lab var price_9 "Ambisome (100 mg/ml per vial)"
  lab var price_10 "Red box for TB drugs (category 1)"
  lab var price_11 "Zinc Sulphate Tablet (20 mg)"

* How charge the fee
  tab e17, gen(charge_method_)

  forvalues n=1/3 {
  replace charge_method_`n' = 1 if charge_method_4==1
  }

  lab var charge_method_1 "Cash"
  lab var charge_method_2 "Credit"
  lab var charge_method_3 "Barter"
  lab var charge_method_4 "Combination of the above"


* Patient Socioeconomic Class
  gen     patient_class=.
  lab var patient_class "Patient Socioeconomic class"

* Difference in fee according to Socioeconomic class
  recode e20 (2=0), gen(diff_fee)
  lab var diff_fee "=1 if provider charges different fee according to socioec. class"

***********************************************
* Familiarity with IT
***********************************************
* Own a computer
  recode f03 (2=0), gen(comp_owner)
  lab var comp_owner "=1 if own a computer"

* Ever use computer
  recode f01 (2=0), gen(comp_ever)
  lab var comp_ever "Have ever used a computer"
  lab val comp_ever yesno 

* Frequency of Usage of Computer
  gen     comp_freq=0
  replace comp_freq=1 if f02<=3 & comp_ever==1
  lab var comp_freq "=1 if use computer at least once a week"
  
* Computer training
  recode f04 (2=0), gen(comp_training)
  lab var comp_training "=1 if has received computer training"

* Training intensity
  gen     comp_train_hs = f05a1
  lab var comp_train_hs "Computer training intensity (hours)"

* Computer in the facility
  recode f07 (2=0), gen(fac_comp)
  lab var fac_comp "=1 if facility has a computer"

* Computer to keep patient records
  recode f08 (2=0) if fac_comp==1, gen(fac_record)
  lab var fac_record "=1 if computerized system for patient record"


***********************************************
* Investment Attitudes
***********************************************
* Risk Aversion
  gen     risk=1 if g01_1==2
  replace risk=2 if g01_1==1 & g01_2==2
  replace risk=3 if g01_1==1 & g01_2==1 & g01_3==2
  replace risk=4 if g01_1==1 & g01_2==1 & g01_3==1
  lab def risk 1 "Risk Lover" 2 "Medium-low risk averse" 3 "Medium-high risk averse" 4 "High Risk averse"
  lab val risk risk
  tab risk, gen(risk_)
  drop risk

* Discount Factor
  gen     patient=4 if g02_1==2
  replace patient=3 if g02_1==1 & g02_2==2
  replace patient=2 if g02_1==1 & g02_2==1  & g02_3==2  
  replace patient=1 if g02_1==1 & g02_2==1  & g02_3==1
  lab define pat 4 "High patient" 3 "Medium-high patient" 2 "Medium-low patient" 1 "Impatient"  
  lab val patient pat
  tab patient, gen(patient_)
  drop patient

***********************************************
* Assets/Infraestructure Index
***********************************************

* Correlation matrix
  corr electricity power_backup room_num bed_day test x_ray fac_comp
  sum  electricity power_backup room_num bed_day test x_ray fac_comp

* Infraestructure index
  pca electricity power_backup room_num bed_day test x_ray fac_comp
  predict iindex, score
  lab var iindex "infraestructure index"

* Infraestructure index (normalized)
  su iindex, meanonly 
  gen niindex = (iindex - r(min)) / (r(max) - r(min)) 

***********************************************
* Saving processed data
***********************************************
  gen last=.
  keep source-last
  drop last
  label data "Provider Interview"
  sort prov_id
  compress
  save "$prodata1\providers_interview_1st", replace
